package _3sort.src;

public class _1BuddleSort {
    public static void main(String[] args) {
        int[] arr = new int[]{5, 8, 6, 3,4, 9, 2, 1, 7};
        for (int i=0; i<arr.length-1; i++) {
            //标记数组是否有序
            boolean isSort=true;
            for (int j = 0; j < arr.length-1-i; j++) {
                //int temp;
                if (arr[j] > arr[j + 1]) {
                    isSort=false;
                    arr[j] = arr[j] ^ arr[j + 1];
                    arr[j + 1] = arr[j]^arr[j + 1];
                    arr[j]=arr[j]^arr[j+1];
                   /* temp = arr[j];
                    arr[j] = arr[j + 1];
                    arr[j + 1] = temp;*/
                }
            }
            //内部排序不做交换，证明已经排好了顺序，这时直接跳出外循环
            if (isSort) {
                break;
            }
        }
        for (int i : arr) {
            System.out.print(i+" ");
        }
    }
}